package com.sl.au.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.Grading;
import com.sl.au.entity.Template;
import com.sl.au.entity.TemplateDetails;

import java.util.List;

public interface TemplateDetailsRepository extends JpaRepository<TemplateDetails, String> {
	
	void deleteByTemplateAndGrading(Template template, Grading grading);
	
	@Query("select distinct a.grading from TemplateDetails a where a.template.id = ?1")
	List<Grading> getGradingByTemplate(String templateId);
	
	@Query("select a from TemplateDetails a where a.template.id = ?1")
	List<TemplateDetails> getByTemplate(String templateId);
	
	@Query("select a from TemplateDetails a where a.template.id = ?1 and a.grading.id = ?2 and a.user is null")
	TemplateDetails getTemplateOfGradingAndNotUser(String templateId, String gradingId);
	
	@Query("select a from TemplateDetails a where a.template.id = ?1 and a.grading.id = ?2")
	List<TemplateDetails> getTemplateOfGrading(String templateId, String gradingId);
	
	@Query("select count(a) from TemplateDetails a where a.grading.id = ?1")
	int getGradingNum(String gradingId);
	
	@Modifying
	@Transactional
	@Query("delete from TemplateDetails a where a.template.id = ?1 and a.grading.id = ?2 and a.user.id = ?3")
	int deleteTemplateDetailsOfUser(String templateId, String gradingId, String userId);
	
	@Modifying
	@Transactional
	@Query("delete from TemplateDetails a where a.template.id = ?1 and a.grading.id = ?2 and a.user.id in (?3)")
	int deleteAllTemplateDetailsOfUser(String templateId, String gradingId, String[] users);
	
	@Modifying
	@Transactional
	@Query("delete from TemplateDetails a where a.template.id = ?1 ")
	int deleteAllTemplateDetailsOfTemplate(String templateId);

}
